package com.qf.producer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

public class ConsumerDemo {


    //集群模式（默认）
//    public static void main(String[] args) throws  Exception{
//        //1.创建消费者Consumer(DefaultMQPushConsumer)，指定消费者组名
//        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test-consumer");
//        //2.指定Nameserver地址
//        consumer.setNamesrvAddr("localhost:9876");
//        //3.订阅(subscribe)主题Topic
//        consumer.subscribe("test-topic","test-tag");
//        //4.设置消费模式(MessageModel),默认集群模式
//        consumer.setMessageModel(MessageModel.CLUSTERING);
//        //5.注册（register）回调函数，处理消息
//        consumer.registerMessageListener(new MessageListenerConcurrently() {
//            @Override
//            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//
//                try {
//                    //消费消息
//                    list.forEach(messageExt -> {
//                        //print message相关信息
//                        String topic = messageExt.getTopic();
//                        String tags = messageExt.getTags();
//                        String keys = messageExt.getKeys();
//                        String msgId = messageExt.getMsgId();
//                        String body = new String(messageExt.getBody());
//
//                        System.out.println("topic："+topic);
//                        System.out.println("tags："+tags);
//                        System.out.println("keys："+keys);
//                        System.out.println("msgId："+msgId);
//                        System.out.println("body："+body);
//                    });
//
//
//                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//                }
//
//            }
//        });
//        //6.启动消费者
//        consumer.start();
//    }

    //广播模式
    public static void main(String[] args) throws  Exception{
        //1.创建消费者Consumer(DefaultMQPushConsumer)，指定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test-consumer");
        //2.指定Nameserver地址
        consumer.setNamesrvAddr("localhost:9876");
        //3.订阅(subscribe)主题Topic
        consumer.subscribe("test-topic","test-tag");
        //4.设置消费模式(MessageModel),默认集群模式
        consumer.setMessageModel(MessageModel.BROADCASTING);
        //5.注册（register）回调函数，处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                try {
                    //消费消息
                    list.forEach(messageExt -> {
                        //print message相关信息
                        String topic = messageExt.getTopic();
                        String tags = messageExt.getTags();
                        String keys = messageExt.getKeys();
                        String msgId = messageExt.getMsgId();
                        String body = new String(messageExt.getBody());

                        System.out.println("topic："+topic);
                        System.out.println("tags："+tags);
                        System.out.println("keys："+keys);
                        System.out.println("msgId："+msgId);
                        System.out.println("body："+body);
                    });


                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }

            }
        });
        //6.启动消费者
        consumer.start();
    }
}
